//
//  XQCityLocationPopView.m
//  JoinPeso
//
//  Created by 小琦 on 2024/12/3.
//

#import "XQCityLocationPopView.h"
#import "XQCityLocationView.h"

@interface XQCityLocationPopView()
@property (nonatomic,strong) UIView *centerView;
@property (nonatomic,strong) UIImageView *imageView;
@property (nonatomic,strong) UILabel *titleLb;
@property (nonatomic,strong) UIButton *closeButton;
@property (nonatomic,strong) UIButton *confimButton;
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) UILabel *topLabel;
@property (nonatomic,strong) UIView *middleView;

@property (nonatomic,strong) XQCityLocationView *proviceView;
@property (nonatomic,strong) XQCityLocationView *cityView;
@property (nonatomic,strong) XQCityLocationView *districtView;

@property (nonatomic, assign) NSInteger proviceIndex;
@property (nonatomic, assign) NSInteger cityIndex;
@property (nonatomic, assign) NSInteger districtIndex;

@end

@implementation XQCityLocationPopView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self initData];
        [self makeUI];
        [self makeConstraints];
    }
    return self;
}

- (void)initData {
    
}

- (void)makeUI {
    [self addSubview:self.centerView];
    [self.centerView addSubview:self.imageView];
    [self.imageView addSubview:self.titleLb];
    [self.imageView addSubview:self.closeButton];
    [self.imageView addSubview:self.topLabel];
    [self.centerView addSubview:self.confimButton];
    [self.imageView addSubview:self.middleView];
    [self.imageView addSubview:self.proviceView];
    [self.imageView addSubview:self.cityView];
    [self.imageView addSubview:self.districtView];
}

- (void)makeConstraints {
    [self.centerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsZero);
        (void)make.center;
    }];
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@0);
        make.left.equalTo(@12);
        make.right.equalTo(@-12);
        make.height.mas_equalTo(404);
    }];
    [self.confimButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.imageView.mas_bottom).offset(10);
        make.left.equalTo(@12);
        make.right.equalTo(@-12);
        make.width.mas_equalTo([UIScreen mainScreen].width - 24);
        make.height.mas_equalTo(45);
        make.bottom.equalTo(@0);
    }];
    [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        (void)make.centerX;
        make.top.equalTo(@12);
    }];
    [self.closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@12);
        make.right.equalTo(@-10);
        make.height.width.mas_equalTo(22);
    }];
    [self.topLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@70);
        make.left.equalTo(@20);
        make.right.equalTo(@-20);
        make.height.mas_equalTo(44);
    }];
    
    [self.proviceView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topLabel.mas_bottom).offset(15);
        make.left.equalTo(@40);
        make.width.mas_equalTo(90);
        make.bottom.equalTo(@-39);
    }];
    
    [self.cityView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.proviceView.mas_top);
        make.left.equalTo(self.proviceView.mas_right);
        make.width.mas_equalTo(130);
        make.bottom.equalTo(@-39);
    }];
    
    [self.districtView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.proviceView.mas_top);
        make.left.equalTo(self.cityView.mas_right);
        make.right.equalTo(@-40);
        make.bottom.equalTo(@-39);
    }];
    
    [self.middleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@220);
        make.height.mas_equalTo(30);
        make.left.equalTo(@20);
        make.right.equalTo(@-20);
    }];
}

#pragma mark - private

- (void)confimClick {
    NSString *provice = self.proviceView.datas[self.proviceIndex];
    NSString *city = self.cityView.datas[self.cityIndex];
    NSString *district = self.districtView.datas[self.districtIndex];
    if (self.confimHandle) {
        self.confimHandle([NSString stringWithFormat:@"%@|%@|%@",provice,city,district]);
    }
}

- (void)closeClick {
    if (self.closeHandle) {
        self.closeHandle();
    }
}

- (void)updateCity:(NSInteger)proviceIndex {
    self.proviceIndex = proviceIndex;
    self.cityIndex = 0;
    self.districtIndex = 0;
    self.cityView.datas = [XQCityManager getCityListAtProvinceIndex:proviceIndex];
    self.districtView.datas = [XQCityManager getDistrictListAtProvinceIndex:proviceIndex cityIndex:self.cityIndex];
}

- (void)updateDistrict:(NSInteger)cityIndex {
    self.cityIndex = cityIndex;
    self.districtIndex = 0;
    self.districtView.datas = [XQCityManager getDistrictListAtProvinceIndex:self.proviceIndex cityIndex:cityIndex];
}

#pragma mark - setter

- (void)setSelectData:(NSString *)selectData {
    NSArray *list = [selectData componentsSeparatedByString:@"|"];
    
    if (list.count == 3) {
        NSInteger proviceIndex = [XQCityManager getProvinceIndex:list[0]];
        NSArray *proviceArr = [XQCityManager getProvinceList];
        [self.proviceView setSelectLocation:proviceArr[proviceIndex]];
        [self updateCity:proviceIndex];
        
        NSInteger cityIndex = [XQCityManager getCityIndex:proviceIndex cityName:list[1]];
        NSArray *cityArr = [XQCityManager getCityListAtProvinceIndex:proviceIndex];
        [self.cityView setSelectLocation:cityArr[cityIndex]];
        [self updateDistrict:cityIndex];
        
        NSInteger districtIndex = [XQCityManager getDistrictIndex:proviceIndex cityIndex:cityIndex districtName:list[2]];
        NSArray *districtArr = [XQCityManager getDistrictListAtProvinceIndex:proviceIndex cityIndex:cityIndex];
        self.districtIndex = districtIndex;
        [self.districtView setSelectLocation:districtArr[districtIndex]];
    }
    
}

#pragma mark - lazy

- (UIView *)centerView {
    if (!_centerView) {
        _centerView = [[UIView alloc] init];
    }
    return _centerView;
}

- (UIImageView *)imageView {
    if (!_imageView) {
        _imageView = [[UIImageView alloc] init];
        _imageView.image = [UIImage imageNamed:@"choose_city_pop"];
        [_imageView setUserInteractionEnabled:YES];
    }
    return _imageView;
}

- (UIButton *)confimButton {
    if (!_confimButton) {
        _confimButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_confimButton setTitle:@"Confirm" forState:UIControlStateNormal];
        [_confimButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _confimButton.titleLabel.font = [UIFont boldSystemFontOfSize:20];
        _confimButton.backgroundColor = [UIColor blueColor];
        _confimButton.layer.cornerRadius = 22.5;
        [_confimButton addTarget:self action:@selector(confimClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _confimButton;
}

- (UILabel *)titleLb {
    if (!_titleLb) {
        _titleLb = [[UILabel alloc] init];
        _titleLb.text = @"Please select an address";
        _titleLb.textColor = [UIColor whiteColor];
        _titleLb.font = [UIFont boldSystemFontOfSize:18];
    }
    return _titleLb;
}

- (UIButton *)closeButton {
    if (!_closeButton) {
        _closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_closeButton setImage:[UIImage imageNamed:@"pop_close"] forState:UIControlStateNormal];
        [_closeButton addTarget:self action:@selector(closeClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _closeButton;
}

- (UILabel *)topLabel {
    if (!_topLabel) {
        _topLabel = [[UILabel alloc] init];
        _topLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
        
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"Washington，DC Providence " attributes:@{NSForegroundColorAttributeName: [UIColor colorWithHexString:@"#2A2D57"]}];
        
        [attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:@"street" attributes:@{NSForegroundColorAttributeName: kBlueTextColor, NSFontAttributeName: [UIFont systemFontOfSize:14 weight:UIFontWeightMedium]}]];

        _topLabel.attributedText = attributedString;
        _topLabel.textAlignment = NSTextAlignmentCenter;
        _topLabel.layer.cornerRadius = 15;
        _topLabel.backgroundColor = [kOrangeColor colorWithAlphaComponent:0.2];
        _topLabel.layer.masksToBounds = YES;
    }
    return _topLabel;
}

- (XQCityLocationView *)proviceView {
    if (!_proviceView) {
        _proviceView = [[XQCityLocationView alloc] init];
        _proviceView.datas = [XQCityManager getProvinceList];
        @weakify(self)
        _proviceView.selectLocationHandle = ^(NSInteger locationIndex) {
            @strongify(self);
            [self updateCity:locationIndex];
        };
    }
    return _proviceView;
}

- (XQCityLocationView *)cityView {
    if (!_cityView) {
        _cityView = [[XQCityLocationView alloc] init];
        _cityView.datas = [XQCityManager getCityListAtProvinceIndex:self.proviceIndex];
        @weakify(self)
        _cityView.selectLocationHandle = ^(NSInteger locationIndex) {
            @strongify(self);
            [self updateDistrict:locationIndex];
        };
    }
    return _cityView;
}

- (XQCityLocationView *)districtView {
    if (!_districtView) {
        _districtView = [[XQCityLocationView alloc] init];
        _districtView.datas = [XQCityManager getDistrictListAtProvinceIndex:self.proviceIndex cityIndex:self.cityIndex];
        @weakify(self)
        _districtView.selectLocationHandle = ^(NSInteger locationIndex) {
            @strongify(self);
            self.districtIndex = locationIndex;
        };
    }
    return _districtView;
}

- (UIView *)middleView {
    if (!_middleView) {
        _middleView = [[UIView alloc] init];
        _middleView.layer.cornerRadius = 8;
        _middleView.backgroundColor = kBlueTextColor;
    }
    return _middleView;
}

@end
